package com.lzc.camera.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lzc.camera.entity.Log;
import com.lzc.camera.entity.LogQuery;
import com.lzc.camera.mapper.LogMapper;
import com.lzc.camera.service.LogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lzc
 * @since 2022-11-20 08:54:54
 */
@Service
public class LogServiceImpl extends ServiceImpl<LogMapper, Log> implements LogService {

    @Autowired
    private LogMapper logMapper;

    @Override
    public int delete() {
        LocalDateTime now = LocalDateTime.now();
        now = now.minus(180, ChronoUnit.DAYS);
        QueryWrapper<Log> wrapper = new QueryWrapper<>();
        wrapper.isNotNull("id");
        wrapper.le("creation_time",now);
        int delete = logMapper.delete(wrapper);
        return delete;

    }

    @Override
    public List<Log> list(LogQuery logQuery, Integer pageSize, Integer pageNum) {
        QueryWrapper<Log> wrapper = new QueryWrapper<>();
        wrapper.isNotNull("id");
        if (logQuery.getId() != null){
            wrapper.eq("id",logQuery.getId());
        }
        if (!StringUtils.isEmpty(logQuery.getPointKey())){
            wrapper.eq("point_key",logQuery.getPointKey());
        }
        if (!StringUtils.isEmpty(logQuery.getIpAddress())){
            wrapper.eq("ip_address",logQuery.getIpAddress());
        }
        if (!StringUtils.isEmpty(logQuery.getFromTime())){
            wrapper.ge("creation_time",logQuery.getFromTime()+8);
        }
        if (!StringUtils.isEmpty(logQuery.getToTime())){
            wrapper.le("creation_time",logQuery.getToTime()+8);
        }
        List<Log> logList = logMapper.selectList(wrapper);
        return logList;
    }
}
